//
//  ViewController.m
//  Tupianliulanqi
//
//  Created by zzn on 2016/12/27.
//  Copyright © 2016年 zzn. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDelegate>
{
    NSMutableData *imdata;
}
@property (weak,nonatomic) IBOutlet UIProgressView *imPro;
@property (weak,nonatomic) IBOutlet UIImageView *imView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.imView.backgroundColor = [UIColor greenColor];
    
}

-(IBAction)onDian:(id)sender {
    NSURL *u = [NSURL URLWithString:@"/Users/nanannana/Desktop/F6D87C5E-F055-4750-951A-729C5FF47627.png"];
    NSURLSession *se = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionTask *tak = [se dataTaskWithURL:u];
    [tak resume];
    self.imPro.progress = 0;
    self.imView.image = nil;
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    self.imView.image = [UIImage imageWithData:imdata];
}


- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    [self.imPro setProgress:(float)dataTask.countOfBytesReceived/(float)dataTask.countOfBytesExpectedToReceive animated:YES];
    if (!imdata) {
        imdata = [NSMutableData new];
    }
    [imdata appendData:data];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
